package interesado;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;

import misc.Logica;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class Pedidos_GUI extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel lblNuevoPedido;
	private JLabel lblAsunto;
	private JLabel lblFundamentacion;
	private JTextField txtAsunto;
	private JTextArea txtFundamentacion;
	private JButton btnTerminar;
	private JButton btnCancelar;
	private JButton btnOtroPedido;

	/**
	* Auto-generated main method to display this JFrame
	*/
	
	private Logica logica;
	private int codigoSolicitud;
	private int numeroPedido=1;
	private RegistrarSolicitud_GUI padre;
	
	public Pedidos_GUI(RegistrarSolicitud_GUI padre, Logica logica, int codigoSolicitud) {
		super();
		initGUI();
		this.logica=logica;
		this.codigoSolicitud=codigoSolicitud;
		this.padre=padre;
	}
	
	private void initGUI() {
		try {
			setTitle("Registrar Pedido");
			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				lblNuevoPedido = new JLabel();
				getContentPane().add(lblNuevoPedido);
				lblNuevoPedido.setText("Nuevo Pedido");
				lblNuevoPedido.setBounds(176, 30, 125, 21);
				lblNuevoPedido.setFont(new java.awt.Font("Segoe UI",0,16));
				lblNuevoPedido.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				lblAsunto = new JLabel();
				getContentPane().add(lblAsunto);
				lblAsunto.setText("Asunto [*]:");
				lblAsunto.setBounds(69, 83, 135, 16);
				lblAsunto.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblFundamentacion = new JLabel();
				getContentPane().add(lblFundamentacion);
				lblFundamentacion.setText("Fundamentación [*]:");
				lblFundamentacion.setBounds(69, 124, 135, 16);
				lblFundamentacion.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				txtAsunto = new JTextField();
				getContentPane().add(txtAsunto);
				txtAsunto.setBounds(229, 80, 236, 23);
			}
			{
				txtFundamentacion = new JTextArea();
				getContentPane().add(txtFundamentacion);
				txtFundamentacion.setBounds(229, 122, 236, 65);
				txtFundamentacion.setBorder(BorderFactory.createTitledBorder(""));
			}
			{
				btnOtroPedido = new JButton();
				getContentPane().add(btnOtroPedido);
				btnOtroPedido.setText("Otro Pedido");
				btnOtroPedido.setBounds(95, 247, 96, 23);
				btnOtroPedido.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						registrarPedido();
					}
				});
			}
			{
				btnTerminar = new JButton();
				getContentPane().add(btnTerminar);
				btnTerminar.setText("Terminar");
				btnTerminar.setBounds(202, 247, 86, 23);
				btnTerminar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						if(txtAsunto.getText().equals("") || txtFundamentacion.getText().equals(""))
							JOptionPane.showMessageDialog(null,"Debe completar los campos correspondientes", "Error", JOptionPane.ERROR_MESSAGE);
						else{
							registrarPedido();
							salir();
						}
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(299, 247, 86, 23);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						padre.eliminarSolicitudFisicamente();
						salir();
					}
				});
			}
			pack();
			this.setSize(542, 331);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	private void registrarPedido(){
		
		String asunto=txtAsunto.getText();
		String fundamentacion=txtFundamentacion.getText();
		int respuesta = JOptionPane.showConfirmDialog(null, "¿Confirma la operación?", "Agregar pedido/s a la solicitud",
                JOptionPane.YES_NO_OPTION);
		
		 //Confirmacion afirmativa, sino volvemos a la pantalla
		 if(respuesta==JOptionPane.YES_OPTION){
		logica.registrarPedido(numeroPedido,codigoSolicitud, asunto,fundamentacion);
		numeroPedido++;
		
		//Limpiar campos
		txtAsunto.setText("");
		txtFundamentacion.setText("");
		 }
	}
	
	private void salir(){
		this.setVisible(false);
		padre.setVisible(true);
		dispose();
	}
}
